/*
 * Copyright 2001-2014 Viewstreet Technologies www.viewstreet.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.viewstreet.beanspice.processor;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclaration;


/**
 * The Class MethodBean. Provides a container for a SpiceMethod java method
 */
public class MethodBean {
	
	/** The type binding. */
	private final ITypeBinding typeBinding;
	
	/** The method dec. */
	private final MethodDeclaration methodDec;
	
	/** The method binding. */
	private final IMethodBinding methodBinding;
	
	/** The parameters. */
	private final ITypeBinding [] parameters;
	
	/** The parameter names. */
	private final List<String> parameterNames ;
	
	/** The is static. */
	private boolean isStatic;
	
	/**
	 * The Constructor.
	 *
	 * @param typeBinding the type binding
	 * @param methodDec the method dec
	 * @throws Exception the exception
	 */
	public MethodBean(ITypeBinding typeBinding,MethodDeclaration methodDec) throws Exception	{
		this.methodDec = methodDec;
		this.typeBinding = typeBinding;
		
		isStatic = false;
		for (Object mod : methodDec.modifiers()) {
			if(mod instanceof Modifier && ((Modifier)mod).toString().equals("static"))	{
				isStatic = true;
				break;
			}
		}
		
		
		
		methodBinding = methodDec.resolveBinding();
		if(methodBinding == null )	{
			throw new Exception("Unable to resolve Binding for "+methodDec.toString());
		}
		 parameters = getMethodBinding().getParameterTypes();
		 parameterNames = loadParameterNames();
	}
	
	/**
	 * Gets the type binding.
	 *
	 * @return the type binding
	 */
	public ITypeBinding getTypeBinding() {
		return typeBinding;
	}
	
	/**
	 * Gets the method dec.
	 *
	 * @return the method dec
	 */
	public MethodDeclaration getMethodDec() {
		return methodDec;
	}
	
	/**
	 * Gets the method binding.
	 *
	 * @return the method binding
	 */
	public IMethodBinding getMethodBinding() {
		return methodBinding;
	}
	
	/**
	 * Gets the parameters.
	 *
	 * @return the parameters
	 */
	public ITypeBinding[] getParameters() {
		return parameters;
	}
	
	/**
	 * Gets the parameter names.
	 *
	 * @return the parameter names
	 */
	public List<String> getParameterNames() {
		return parameterNames;
	}
	
	/**
	 * Load parameter names.
	 *
	 * @return the list< string>
	 */
	private List<String> loadParameterNames()	{
		List<String> parmNames = new ArrayList<String>(); 
		for (Object parameter : getMethodDec().parameters()) {
	        VariableDeclaration variableDeclaration = (VariableDeclaration) parameter;
	        String name = variableDeclaration.getStructuralProperty(SingleVariableDeclaration.NAME_PROPERTY).toString();
	       	parmNames.add(name);
	    }
		return parmNames;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString()	{
		return methodBinding.getKey();
	}
	
	/**
	 * Checks if is static.
	 *
	 * @return true, if checks if is static
	 */
	public boolean isStatic() {
		return isStatic;
	}
	
	
	
}